const cp = require('cp');
require('./page.css');
let moduleExport = {};

let vm = avalon.define({
  $id: 'mc_step3_detail',
  status: -1, // 用于存储合同签署状态
  payed: -1,
  tempid: 0,
  tempinfo: [],
  temporg: [
    {
      title: '武汉瑞傲科技有限公司',
      sign: '这里是一段公司描述',
      mbrcount: 0,
      actcount: 0,
      followcount: 0,
      createtime: 1505119890000,
      logo: 'http://op4axvn75.bkt.clouddn.com/photo_company.png',
    },
  ],
  tempcms: {
    text: '这里是公司简介',
  },
  formatZero: (e = 0) => {
    return e;
  },
  formatTime: (e, type) => {
    return cp.formatTime(e, type);
  },
  getStatus: (status, payed, type) => {
    if (status > 2 || status === 2) {
      if (type === 'class') {
        return payed === -1 ? 'current' : 'gray';
      } else {
        return payed === -1 ? '立即使用' : '已购买';
      }
    } else {
      return type === 'class' ? 'gray' : '请先签署合同';
    }
  },
  gotoPay: () => {
    if ((vm.status > 2 || vm.status === 2) && vm.payed === -1) {
      window.location.href = cp.PAGE_ROOT + '/index/tickets/page.html#/' + Math.floor(Math.random() * 100000) + vm.tempinfo.goodsid;
    }
  },
  bindGotoId: (e, id) => {
    $('.u-nav .item').removeClass('current');
    $(e.target).addClass('current');
    document.getElementById(id).scrollIntoView();
  },
  init: (tempid) => {
    vm.tempid = tempid;
    cp.ajaxUrl({
      controller: 'shopskt',
      cmd: 'com.rel.porg.pb.com_sel_temp_req',
      async: false,
      data: {
        shoptempid: tempid,
      },
      success: res => {
        res.temp.banners = JSON.parse(res.temp.banners);
        res.temp.preimgs = JSON.parse(res.temp.preimgs);
        vm.tempinfo = res.temp;
        vm.tempcms = res.cms;
        if (res.status) {
          vm.status = res.status;
        }
        cp.ajaxUrl({
          controller: 'shopskt',
          cmd: 'com.rel.porg.pb.shop_sel_contact_req',
          data: {},
          success: res => {
            vm.status = res.info.process;
          },
          fail: res => {
            cp.layerMsg(res.desc);
          },
        });
        cp.ajaxUrl({
          controller: 'shopskt',
          cmd: 'com.rel.porg.pb.shop_sel_usershop_req',
          data: {
            shoptempid: tempid,
          },
          success: res => {
            vm.payed = 1;
          },
          fail: res => {
            vm.payed = -1;
          },
        });
      },
      fail: res => {
        cp.layerMsg(res.desc);
      },
    });
  },
});

moduleExport.content = require('./content.html');
moduleExport.vm = vm;
module.exports = moduleExport;
